package org.wjy.post.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.wjy.post.dao.UserDao;
import org.wjy.post.other.Json;
import org.wjy.post.service.UserService;
import org.wjy.post.vo.User;


import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;

/**     
 * 类名称：LoginAction   
 * 类描述：用户控制器的实现
 * 创建人：anan   
 * 创建时间：2012-12-21 下午11:17:36   
 * 修改人：anan  
 * 修改时间：2012-12-21 下午11:17:36   
 * 修改备注：   
 * @version        
 * */
@Namespace("/post")
public class UserAction extends ActionSupport {
	private static final long serialVersionUID = -2266695172069461659L;
	private static final Logger logger = Logger.getLogger(UserAction.class);
	@Resource(name="userService")
	private UserService userService;
	private int id;
	private String customerNo;
	private String customerName;
	private String telephone;
	private String address;
	private boolean flag;
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public String getCustomerNo() {
		return customerNo;
	}
	public void setCustomerNo(String customerNo) {
		this.customerNo = customerNo;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public User getUser(){
		User user=new User();
		user.setId(this.id);
		user.setTelephone(this.telephone);
		user.setAddress(this.address);
		user.setCustomerName(this.customerName);
		user.setCustomerNo(customerNo);
		return user;
	}
	
	@Action(value = "loginAction", results = {
			@Result(name = "success", location = "/loginSuss.jsp"),
			@Result(name = "failer", location = "/loginFailer.jsp") })
	public String execute(){
//		boolean flag = userService.checkUser(user);
//		if(flag){
//			return "success";
//		}
		return "failer";
	}
	
	
	@Action(value = "deleteuserAction", results = { })
	public void deluser(){
		
		Json ret=new Json();
		ret.setMsg("删除失败!!!");
		flag=userService.delUser(this.id);
		if(flag){
			ret.setSuccess(true);
			ret.setMsg("删除成功!!!");
		}
		String json = JSON.toJSONString(ret);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Action(value = "adduserAction", results = {})
	public void adduser(){
		User user=getUser();
		
		boolean flag=userService.addUser(user);
		Json ret=new Json();
		ret.setMsg("添加失败");
		if(flag){
			ret.setMsg("添加成功");
			ret.setSuccess(true);
		}
		try {			
			
			
			String json = JSON.toJSONString(ret);
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			
		};
	}
	
	@Action(value = "updateAction", results = {})
	public void update(){
		User user=getUser();
		//System.out.println(this.customerNo);
		try {			
			boolean flag=userService.update(user);
			Json ret=new Json();
			if(flag){
				ret.setSuccess(true);
				ret.setMsg("修改成功");
				ret.setObj(null);	
			}
			else{
				ret.setSuccess(true);
				ret.setMsg("修改成功");
				ret.setObj(null);	
			}
			String json = JSON.toJSONString(ret);
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			
		};
	}
	
	
	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
}
